package com.iplatform.iplatformzuul.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author Dair
 * @since
 */
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
	
	@Autowired
	private DiscoveryClient discoveryClient;
	@Value("${spring.application.name}")
	private String applicationName;
	
	
	private static final String PLATFORM_NAME = "iplatform";
	
	@Override
	public List<SwaggerResource> get() {
		List<SwaggerResource> resources = new ArrayList<>();
		// 排除自身，将其他的服务添加进去
		discoveryClient.getServices().stream().forEach(name -> {
			Optional<ServiceInstance> instanceOptional = discoveryClient.getInstances(name).stream().findFirst();
			if(name.contains(PLATFORM_NAME)){
				if (!name.equals(applicationName) && instanceOptional.isPresent() && instanceOptional.get().getMetadata().containsKey("context-path")) {
					String contexPath = instanceOptional.get().getMetadata().get("context-path");
					resources.add(swaggerResource(name, "/" + name + contexPath + "/v2/api-docs", "2.0"));
				} else if (name.equals(applicationName)) {
					resources.add(swaggerResource(name, "/" + "/v2/api-docs", "2.0"));
				} else {
					resources.add(swaggerResource(name, "/" + name + "/v2/api-docs", "2.0"));
				}
			}
			
		});
		return resources;
	}
	
	private SwaggerResource swaggerResource(String name, String location, String version) {
		SwaggerResource swaggerResource = new SwaggerResource();
		swaggerResource.setName(name);
		swaggerResource.setLocation(location);
		swaggerResource.setSwaggerVersion(version);
		return swaggerResource;
	}
	
}